%%%%%%%%%%%%%%%%%%%%%%
%%%%% Produce Figure 1
%%%%%%%%%%%%%%%%%%%%%%



%Upper three panels



clear all; 
close all;


para;

OptContract;

SB;

Solve;




fFigure=figure(1); hold on;


 subplot(1,3,1); hold on
 plot(sol.x,sol.y(1,:), 'k', 'Linewidth',3,'Handlevisibility','off');
plot([V0 V0], [min(sol.y(1,:)) max(sol.y(1,:))], 'r--','Linewidth',1.5)
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$F(V)$','Interpreter','latex')
 title('A: Value Function','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 
 
  subplot(1,3,2); hold on
 plot(sol.x,vA, 'k', 'Linewidth',3,'Handlevisibility','off');
plot([V0 V0], [min(vA) max(vA)], 'r--','Linewidth',1.5)
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$a(V)$','Interpreter','latex')
  title('B: Monitoring Effort','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 
 
  subplot(1,3,3); hold on
 plot(sol.x,vC, 'k', 'Linewidth',3,'Handlevisibility','off');
plot([V0 V0], [min(vC) max(vC)], 'r--','Linewidth',1.5)
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$c(V)$','Interpreter','latex')
 title('C: Payouts','Interpreter', 'LaTex', 'FontSize',13)

 axis tight;

resize_plot(fFigure,11,2.5,13);


print('valfun','-depsc')


%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Lower Three Panels 
%%%%%%%%%%%%%%%%%%%%%%%%

clear all; close all;
para;
options = odeset('AbsTol',1e-13,'RelTol',1e-13);


OptContract;

SB;

Solve;

T=40;
vTime=linspace(0,T,1000);

V0=kappa*q;

[m,ind]=min(abs(sol.x-V0));
solVTime=ode15s(@OdeTime,[0 T],[V0 F0 dF0 beta0],options,sol.x, sol.y, vA, gamma,phi,q,Lambda,r,delta, VB,vC,vD,solQuant.x,betaB );

vbetat=zeros(length(solVTime.x),1);
vtaut=zeros(length(solVTime.x),1);
vAt=zeros(length(solVTime.x),1);

%Calculate value function, effort as a function of time
for t=1:length(solVTime.x)
    
    [m,ind]=min(abs(solVTime.y(1,t)-sol.x));
    vFt(t)=solVTime.y(2,t);
    
    
    vAt(t)=(vFt(t)-solVTime.y(3,t)*(solVTime.y(1,t)+phi)-phi*(gamma-r))/phi;
     if (minimal==1)
            vAt(t)=vA(ind);
     end
    
   
    
end



fFigure=figure(1); hold on;

 subplot(1,3,3); hold on
 plot(solVTime.x,solVTime.y(1,:), 'k', 'Linewidth',3,'Handlevisibility','off');
 plot(solVTime.x,ones(length(solVTime.x),1)*VB, 'r:', 'Linewidth',1.5);
 leg2=legend('$V^B(q^*)$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$t$','Interpreter','latex')
 ylabel('$V_t$','Interpreter','latex')
 title('F: Screening Incentives','Interpreter', 'LaTex', 'FontSize',13)
 xlim([0 T]);
 ylim([VB-0.05 V0])

 
 
 subplot(1,3,1); hold on
 plot(solVTime.x,solVTime.y(2,:), 'k', 'Linewidth',3,'Handlevisibility','off');
 plot(solVTime.x,ones(length(solVTime.x),1)*FB, 'r:', 'Linewidth',1.5);
 leg2=legend('$F^B(q^*)$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$t$','Interpreter','latex')
 ylabel('$F_t$','Interpreter','latex')
 axis tight;
 title('F: Value Function','Interpreter', 'LaTex', 'FontSize',13)

 
 
 subplot(1,3,2); hold on
 plot(solVTime.x,smooth(vAt), 'k', 'Linewidth',3,'Handlevisibility','off');
 plot(solVTime.x,ones(length(solVTime.x),1)*(min(vAt)), 'r:', 'Linewidth',1.5);
 leg2=legend('$a^B(q^*)$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$t$','Interpreter','latex')
 ylabel('$a_t$','Interpreter','latex')
 title('E: Monitoring Effort','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;



resize_plot(fFigure,11,2.5,13.5);


print('valfunt','-depsc')


